import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ContextReplace {

    public static void findFiles(String baseDirName, String targetFileSuffixName,
                                 List fileList) throws IOException,
            InterruptedException {
        String tempName = null;
        File baseDir = new File(baseDirName);
        if (!baseDir.exists() || !baseDir.isDirectory()) {
            log.error("{}", "error folder or is not a folder..");
        } else {
            String[] filelist = baseDir.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(baseDirName + "\\" + filelist[i]);
                if (!readfile.isDirectory()) {
                    if (readfile.getName().contains(targetFileSuffixName)) {
                        String[] array = ContextReplace.read(readfile);
                        if ("change".equalsIgnoreCase(array[1])) {
                            ContextReplace.write(array[0], readfile);
                            fileList.add(readfile.getAbsoluteFile());
                        }
                    }
                } else if (readfile.isDirectory()) {
                    findFiles(baseDirName + "\\" + filelist[i], targetFileSuffixName,
                            fileList);
                }
            }
        }
    }

    public static String[] read(File src) {
        String[] array = new String[2];
        String hasChangeText = "unchange";
        String line = null;
        FileInputStream in = null;
        StringBuilder sb = null;
        try {
            in =new FileInputStream(src);
            byte[] b = new byte[in.available()];
            in.read(b);
            sb = new StringBuilder(new String(b));
            Pattern r = Pattern.compile(RegularPattern.newJoinColumnPattern.getDesc());
            Matcher m = r.matcher(sb);
            while (m.find()) {
                sb.replace(m.start(1), m.end(1), "false");
                hasChangeText = "change";
                log.info("replaced String: {} in File {}", m.group(0),  src.getAbsolutePath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        array[0] = sb.toString();
        array[1] = hasChangeText;
        return array;
    }
    public static boolean write(String cont, File dist) {
        OutputStreamWriter writer = null;
        try {
            writer = new OutputStreamWriter(
                    new FileOutputStream(dist), "UTF-8");
            writer.write(cont);
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException,
            InterruptedException {
        List resultList = new ArrayList();
        Long startTime = System.currentTimeMillis();
        ContextReplace.findFiles(args[0], FileSuffix.JAVA.getDesc(), resultList);
        Long endTime = System.currentTimeMillis();
        if (resultList.size() == 0) {
            log.warn("{}", "No match file find.");
        } else {
            log.info("replace in {} files, use time in {} second", resultList.size(), (endTime - startTime)/1000);
        }
    }
    
}